package org.duang.kit;

import org.duang.server.DuangServer;

import com.aliyun4j.utils.Tools;

public class ServerKit {
	private String webAppDir;
	private int port;
	private String context;
	private int minPort = 8760;
	private int maxPort = 8769;
	private static ServerKit dserver;
	public static ServerKit duang() {
		if (dserver == null) {
			dserver = new ServerKit();
		}
		return dserver;
	}
	
	public ServerKit webAppDir(String webAppDir){
		if(ToolsKit.isEmpty(webAppDir)) throw new IllegalStateException("webAppDir is null");
		this.webAppDir = webAppDir;
		return this;
	}
	
	public ServerKit port(int port){
		if(port < minPort || port > maxPort) throw new IllegalStateException("port only range is "+minPort+"~"+maxPort);
		this.port = port;
		return this;
	}
	
	public ServerKit context(String context){
		if(ToolsKit.isEmpty(context)) throw new IllegalStateException("context is null");
		this.context = context;
		return this;
	}
	
	public void run() {
		if(Tools.isEmpty(webAppDir)) webAppDir = "webapp";
		if( Tools.isEmpty(context) ) context = "/";
		if(port == 0) port = minPort;
		DuangServer ds = new DuangServer();
		ds.init(webAppDir, port, context);
		ds.start();
	}
}
